/*
 * LanguageManager.java
 *
 * Created on 15 de octubre de 2006, 1:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ub.es.ubictionary.logic;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import ub.es.ubictionary.io.Reader;
import ub.es.ubictionary.io.Writer;

/**
 *
 * @author aram
 */
public class LanguageManager {
    ArrayList languages;
    /** Creates a new instance of LanguageManager */
    
    public LanguageManager() {
        languages = new ArrayList(5);
    }
    
    public boolean addLanguage(String name, String [] words){
        Language l = getLanguage(name);
        if (l != null)
            l.setWords(words);
        else
            languages.add(new Language(name, words));
        
        return saveLanguagesOnFile();
    }
    public void loadLanguages() throws FileNotFoundException, UnsupportedEncodingException, IOException{
        ArrayList lst = new ArrayList();
        lst = Reader.getLines("interface");
        for (int i = 0; i < lst.size(); i++){
            String l = (String) lst.get(i);
            String [] ls = l.split("#", 2);
            if (ls.length > 1){
                String [] words = ls[1].split("\\|");
                languages.add(new Language(ls[0], words));
            }
        }
    }
    
    public ArrayList getLanguages(){
        return languages;
    }
    
    public int getCuantityOfLanguages(){
        return languages.size();
    }
    
    public Language getLanguage(String name){
        for(int i = 0; i < languages.size(); i++){
            if ( ((Language)languages.get(i)).getName().compareTo(name) ==0 ){
                return (Language) languages.get(i);
            }
        }
        return null;
    }
    public Language openLanguage(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException, InvalidLanguageFileException{
        ArrayList lst = new ArrayList();
        lst = Reader.getLinesFromExternalFile(file);
        if (lst.size() > 0){
            String l = (String) lst.get(0);
            String [] ls = l.split("#", 2);
            if (ls.length > 1){
                String [] words = ls[1].split("\\|");
                return new Language(ls[0], words);
            }else{
                throw new InvalidLanguageFileException();
            }
        }else{
            throw new InvalidLanguageFileException();
        }
        
    }
    
    public boolean deleteLanguage(String name){
        Language l = getLanguage(name);
        if (l==null)
            return false;
        languages.remove(l);
        return saveLanguagesOnFile();
    }
    
    private boolean saveLanguagesOnFile(){
        ArrayList ar = new ArrayList();
         for (int i = 0; i < languages.size(); i++){
             String salida = "";
             Language lang = (Language) languages.get(i);
             salida = lang.getName() + "#";
             String w[] = lang.getWords();
             for (int j = 0; j < w.length; j++){
                 if (j == w.length-1){
                     salida = salida + w[j];
                 }else{
                     salida = salida + w[j] + "|";
                 }
             }
             ar.add(salida);
         }
        try {
            Writer.createFile("interface",ar);
        } catch (UnsupportedEncodingException ex) {
            return false;
        } catch (FileNotFoundException ex) {
            return false;
        } catch (IOException ex) {
            return false;
        }
        return true;
    }
}
